const db = wx.cloud.database();
// pages/Search/Search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
	resultArr: [],
	inputValue: '',
	isShow: false
  },
  
  timeTime: 0,
  
  handleInput(e){
	  let inputValue = e.detail.value;
	  if(!inputValue.trim()){
		  return;
	  }else{
		this.setData({
		  inputValue
		})
		if(this.data.inputValue.length === 1){
			clearTimeout(this.timeTime);
			this.setData({
				resultArr: []
			})
			return;
		}else{
			//防抖
			clearTimeout(this.timeTime);
			this.timeTime = setTimeout(() => {
				this.getSearchGoods(this.data.inputValue);
			},1000)
		}

	  }
  },
  
  isFocus(e){
	this.setData({
		isShow: true
	})
  },
  lostFocus(e){
	this.setData({
  		isShow: false
  	})
  },
  
  
  cancelAll(){
	clearTimeout(this.timeTime);
	this.setData({
		resultArr: [],
		inputValue:''

	})
	this.lostFocus();
  },
  
  
  async getSearchGoods(query){

		const result = await wx.cloud.callFunction({
		   name: 'searchInput',
		   data: {
		     inputValue: query
		   }
		 }).then(res => {
		   this.setData({
		     resultArr: res.result.data
		   })
		 })
	
}
})